package com.hp.bon.sgw.threads;

import java.util.ArrayList;

import com.hp.bon.sgw.Constants;
import com.hp.bon.sgw.snmp.SgwNMContext;

abstract class AnanlysBase
{
  public String name;
  public long _counterSuccess;
  public long _counterTimeout;
  public long _counterOther;
  public long _counterNotSure;
  Constants.CheckStatus lastSureStatus;
  public long lastSureStatusUpdateTime;
  Constants.CheckStatus curStatus;
  public long updateTime;
  public String errMsg;

  public AnanlysBase(String name)
  {
    this.name = name;
    this.lastSureStatus = Constants.CheckStatus.OK;
    this.lastSureStatusUpdateTime = System.currentTimeMillis();
  }

  public void init() {
    this._counterSuccess = 0L;
    this._counterTimeout = 0L;
    this._counterOther = 0L;
    this._counterNotSure = 0L;
    this.errMsg = null;
  }

  public void calcStatus()
  {
    long total = this._counterSuccess + this._counterOther + this._counterTimeout;

    if (total < 2L) {
      this.curStatus = Constants.CheckStatus.NotSure;
      return;
    }
    if (this._counterSuccess == total)
      this.curStatus = Constants.CheckStatus.OK;
    else if (this._counterTimeout == total)
      this.curStatus = Constants.CheckStatus.TIMEOUT;
    else if (this._counterOther == total)
      this.curStatus = Constants.CheckStatus.ERROR;
    else {
      this.curStatus = Constants.CheckStatus.NotSure;
    }
    this.updateTime = System.currentTimeMillis();

    if ((this.curStatus != Constants.CheckStatus.NotSure) && (this.lastSureStatus != this.curStatus)) {
      this.errMsg = (getType() + this.name + " change status from " + this.lastSureStatus + " to " + this.curStatus);
      Constants.LOG.info(this.errMsg);
      if ((this.curStatus == Constants.CheckStatus.TIMEOUT) || (this.curStatus == Constants.CheckStatus.ERROR)) {
        Constants.LOG.warn("send trap:[" + getType() + "," + this.name + "," + this.curStatus + "]");
        SgwNMContext.notifyBusiAlarm(getType(), this.name, this.curStatus);
      }
      this.lastSureStatus = this.curStatus;
      this.lastSureStatusUpdateTime = this.updateTime;
    }
  }

  public void collectWaring(ArrayList<String> warningList, long begin) {
    if ((this.curStatus != Constants.CheckStatus.NotSure) && (this.lastSureStatusUpdateTime >= begin)) {
      warningList.add(this.errMsg);
    }
    else {
      collectChildWarning(warningList, begin);
    }

    for (String msg : warningList)
      Constants.LOG.warn(msg);
  }

  protected abstract void collectChildWarning(ArrayList<String> paramArrayList, long paramLong);

  protected abstract String getType();

  protected void counterByStatus(Constants.CheckStatus status)
  {
    switch (status) {
    case ERROR:
      this._counterSuccess += 1L;
      break;
    case NotSure:
      this._counterNotSure += 1L;
      break;
    case TIMEOUT:
      this._counterTimeout += 1L;
      break;
    case OK:
      this._counterOther += 1L;
    }
  }
}